using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLThuHocPhi
{
    public partial class FrmDKHoc : Form
    {
        public FrmDKHoc()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            lbThanhtien.Text = "";
            for (int i = listBox2.Items.Count - 1; i >= 0; i--)
            {
                listBox2.Items.RemoveAt(i);
            }
            try
            {
                ds = dbt.ThongTinSV(txtmasv.Text);
                LbTensv.Text = ds.Tables[0].Rows[0].ItemArray[0].ToString();
                lbKhoa.Text = ds.Tables[0].Rows[0].ItemArray[1].ToString();
                lbKhoahoc.Text = ds.Tables[0].Rows[0].ItemArray[2].ToString();
                if (comboBox2.Text == "Tất cả")
                    loadlistmonhoc();
                else
                    loadlistmonhoc1();
            }
            catch (Exception ex)
            {

                MessageBox.Show("Khong tim thay sinh vien");
            }
        }
        QLThuHocPhi.Database.DBTool dbt = new QLThuHocPhi.Database.DBTool();
        DataSet ds = new DataSet();
        private void FrmDKHoc_Load(object sender, EventArgs e)
        {
            dbt.loadform(QLThuHocPhi.Database.DBTool.username);
            lbNhanvienthu.Text = QLThuHocPhi.Database.DBTool.tennhanvien;
            //   ds = dbt.ThongTinSV();
            comboBox1.SelectedIndex = 0;
            lbngaythu.Text = DateTime.Now.ToString();
            comboBox2.SelectedIndex = 0;
            DataSet dsss = new DataSet();
            dsss = dbt.IDmuchocphimax();
            int id = int.Parse(dsss.Tables["idmuchocphi"].Rows[0].ItemArray[0].ToString());
            int id1 = id + 1;
            txtmahoadon.Text = "" + id1;
            txtmahoadon.Enabled = false;
            comboBox1.Text = "Đóng học phí";
          
        }
       
        public void loadlistmonhoc1()
        {
            DataSet ds = new DataSet();
            ds = dbt.LoadMonHoc1(int.Parse(txtmasv.Text));
            this.listBox1.DataSource = ds.Tables["MonHoc1"];
            this.listBox1.DisplayMember = "TenMonHoc";
            this.listBox1.ValueMember = "MaMon";

        }
        public void loadlistmonhoc()
        {
            DataSet ds = new DataSet();
            ds = dbt.LoadMonHoc(txtmasv.Text);
            this.listBox1.DataSource = ds.Tables["MonHoc"];
            this.listBox1.DisplayMember = "TenMonHoc";
            this.listBox1.ValueMember = "MaMon";
           
        }
      //  private int tongtien = 0;
        private void button2_Click(object sender, EventArgs e)
        {

            string tg = listBox1.Text;
            string k = "Đóng tiền thi lại";
           // int status = 1;
            foreach (string a in listBox2.Items) {
                if (a.Equals(tg)) {          
                    return;
                }
            }
            
                listBox2.Items.Add(tg);            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string tg = listBox2.Text;
            string k = "Đóng tiền thi lại";
            listBox2.Items.Remove(tg);
            try
            {
                if (comboBox1.Text != k)
                {
                    DataSet ds3 = new DataSet();
                    ds3 = dbt.TinhTien(tg);
                    int tien = int.Parse(ds3.Tables["thanhtien"].Rows[0].ItemArray[0].ToString());

                 //   lbThanhtien.Text = tongtien.ToString();
                }
               
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Hãy chọn tên môn học muốn hủy");
            }
            
            //listBox2.Items.Add(tg);
            //listBox1.SelectedItem = false;

        }

        private void button4_Click(object sender, EventArgs e)
        {
            int tongtien = 0;
            string k ="Đóng tiền thi lại";
           if(  MessageBox.Show(comboBox1.Text,"Thong bao",MessageBoxButtons.OKCancel)==DialogResult.OK){

             //  MessageBox.Show("ok");
               if (comboBox1.Text != k)
               {
                   foreach (string s in listBox2.Items)
                   {
                       ds = dbt.TinhTien(s);
                     //  ds.WriteXml("C:/tinhtien.xml");
                       tongtien += int.Parse(ds.Tables["Thanhtien"].Rows[0].ItemArray[0].ToString());
                   }
               }
               else {
                   foreach (string s in listBox2.Items)
                   {
                       ds = dbt.TinhTienThiLai(s);
                      // ds.WriteXml("Tinhtienthilai.xml");
                       tongtien += int.Parse(ds.Tables["Thanhtien1"].Rows[0].ItemArray[0].ToString());
                   }
               }
               lbThanhtien.Text = tongtien.ToString();
             }


        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // tongtien = 0;
        }

        private void button5_Click(object sender, EventArgs e)
        {
           /* dbt.CreateHoadon(int.Parse(txtmahoadon.Text), int.Parse(txtmasv.Text), int.Parse(lbThanhtien.Text), lbNhanvienthu.Text, DateTime.Parse(lbngaythu.Text), comboBox1.Text);
            for (int i = 0; i < listBox2.Items.Count; i++)
            {
                DataSet dss = new DataSet();
                dss = dbt.LayIDMonHoc(listBox2.Items[i].ToString());
                int mamon = int.Parse(dss.Tables[0].Rows[0].ItemArray[0].ToString());
                MessageBox.Show(mamon.ToString());
                dbt.CreateChitietHoadon(int.Parse(txtmahoadon.Text), mamon);
            }*/
            FormHoadonSV f = new FormHoadonSV();
            f.Mahoadon = txtmahoadon.Text;
            f.masinhvien = txtmasv.Text;
            f.tensv = LbTensv.Text;
            
            f.khoa = lbKhoa.Text;
            f.khoahoc = lbKhoahoc.Text;
            f.nhanvien = lbNhanvienthu.Text;
            f.sotien = lbThanhtien.Text;
            f.trangthai = comboBox1.Text;
            string tenmonhoc = "";
            for (int i = 0; i < listBox2.Items.Count; i++) {
                tenmonhoc += ","+listBox2.Items[i].ToString();
            }
            f.monhoc = tenmonhoc.Substring(1,tenmonhoc.Length-1);
                f.Show();
           
        }
    }
}